//
//  LSZMyViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/14.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZMyViewController.h"
#import <BMKLocationkit/BMKLocationComponent.h>
#import "JPUSHService.h"

@interface LSZMyViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,BMKLocationManagerDelegate>
{
    NSArray * arrayTitle;
}
@property(nonatomic,strong) UITableView * tableViewMy;
@property(nonatomic,strong) UIImageView * imageViewHeader;
@property(nonatomic,strong) UILabel * labelTitle;
@property(nonatomic,strong) UILabel * labePhone;
@property(nonatomic,strong) UILabel * labelMinistry;
@property(nonatomic,strong) UIImageView * myHeader;
@property(nonatomic,strong) BMKLocationManager * locationManager;
@property(nonatomic,strong) NSString * stringAddress;
@end

@implementation LSZMyViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=NO;
    
    if ([[_MyCenter realName] isEqualToString:@""]) {
        [LSZRequestData initWithPersonalCenter:^(BOOL ISoK, NSString * string, NSDictionary * dic) {
            if (ISoK == YES) {
                self.labelTitle.text = [_MyCenter realName];
                self.labePhone.text = [_MyCenter mobilePhone];
                self.labelMinistry.text = [_MyCenter branch_name];
                [self.myHeader sd_setImageWithURL:[NSURL URLWithString:[_MyCenter headUrl]] placeholderImage:[UIImage imageNamed:@"默认"]];
            }else{
                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
//                if ([string isEqualToString:@"重复登录"]) {
//                    [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZMyViewController"];
//                }else{
//                    [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
//                }
            }
        }];
    }else{
        self.labelTitle.text = [_MyCenter realName];
        self.labePhone.text = [_MyCenter mobilePhone];
        self.labelMinistry.text = [_MyCenter branch_name];
//        NSLog(@"%@>>>",[_MyCenter headUrl]);
        if ([(NullStringjudgment(NSStr([_MyCenter headUrl]))) isEqualToString:[NSString stringWithFormat:@"%@%@",URLTotal,@"<null>"]]) {
            self.myHeader.image = [UIImage imageNamed:@"头像默认"];
        }else{
            [self.myHeader sd_setImageWithURL:[NSURL URLWithString:[_MyCenter headUrl]] placeholderImage:[UIImage imageNamed:@"默认"]];
        }
    }
    if ([(NullStringjudgment(NSStr([_MyCenter headUrl]))) isEqualToString:[NSString stringWithFormat:@"%@%@",URLTotal,@"<null>"]]) {
        self.myHeader.image = [UIImage imageNamed:@"头像默认"];
    }else{
//        NSLog(@"%@>>>>>",[_MyCenter headUrl]);
        [self.myHeader sd_setImageWithURL:[NSURL URLWithString:[_MyCenter headUrl]] placeholderImage:[UIImage imageNamed:@"默认"]];
    }
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
-(void)addNotificationCenter
{
    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZMyViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
}
-(void)setUiDingWei
{
    //初始化实例
   _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = self;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //设置是否允许后台定位
    //_locationManager.allowsBackgroundLocationUpdates = YES;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 10;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 10;
    [_locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
        //获取经纬度和该定位点对应的位置信息
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
        }
        if (location) {//得到定位信息，添加annotation
            
            if (location.location) {
                NSLog(@"LOC = %@",location.location);
            }
            if (location.rgcData) {
//                NSLog(@"rgc = %@",[location.rgcData description]);
                self.stringAddress = [NSString stringWithFormat:@"%@%@%@%@%@%@",location.rgcData.country,location.rgcData.province,location.rgcData.city,location.rgcData.district,location.rgcData.street,location.rgcData.locationDescribe];
                NSLog(@"%@>>>>地理位置",self.stringAddress);
            }
        }
        NSLog(@"netstate = %d",state);
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    [self setUI];
//    [self addNotificationCenter];

    [self.tableViewMy registerClass:[LSZHeaderMyCell class] forHeaderFooterViewReuseIdentifier:@"LSZHeaderMyCell"];
    [self.tableViewMy registerClass:[LSZMyFootCell class] forHeaderFooterViewReuseIdentifier:@"LSZMyFootCell"];
    [self.tableViewMy registerClass:[LSZMyAndImageViewTableViewCell class] forCellReuseIdentifier:@"LSZMyAndImageViewTableViewCell"];
    
    NSArray * arrayOne = @[@"我的收藏",@"历史记录",@"我的查询",@"设置"];
    NSArray * arrayTwo = @[@"关于"];
    arrayTitle = @[arrayOne,arrayTwo];
    [self setUiDingWei];
//    [self.view tapGesture:^(UIGestureRecognizer *ges) {
//        [self.view endEditing:YES];
//    }];
}
-(void)setUI
{
    self.imageViewHeader = [[UIImageView alloc]init];
    self.imageViewHeader.backgroundColor = [UIColor yellowColor];
    self.imageViewHeader.contentMode = UIViewContentModeScaleAspectFill;
    self.imageViewHeader.frame = CGRectMake(0, -NAVIGATION_BAR_HEIGHT-150, [[UIScreen mainScreen]bounds].size.width,NAVIGATION_BAR_HEIGHT+150);
    self.imageViewHeader.clipsToBounds = YES;//需要添加这个不然的话,第一行会有遮盖
    self.imageViewHeader.image = [UIImage imageNamed:@"我的区头背景"];
    [self.tableViewMy addSubview:self.imageViewHeader];
    
    UIButton * butQian = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-10-60, -180, 50, 50)];
    [butQian setImage:[UIImage imageNamed:@"签到"] forState:UIControlStateNormal];
    [butQian addTarget:self action:@selector(clickQianDao) forControlEvents:UIControlEventTouchUpInside];
    [self.tableViewMy addSubview:butQian];
    
    self.myHeader = [[UIImageView alloc]init];
    self.myHeader.frame = CGRectMake((ScreenWidth-66)/2, -150, 66, 66);
    if ([_MyCenter headUrl].length == 0) {
        self.myHeader.image = [UIImage imageNamed:@"头像默认"];
    }else{
        [self.myHeader sd_setImageWithURL:[NSURL URLWithString:[_MyCenter headUrl]] placeholderImage:[UIImage imageNamed:@"默认"]];
    }
    [self.myHeader.layer setMasksToBounds:YES];
    [self.myHeader.layer setCornerRadius:self.myHeader.frame.size.width/2];
    [self.tableViewMy addSubview:self.myHeader];
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.textColor = [UIColor whiteColor];
    self.labelTitle.font = FONT_(15);
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    [self.tableViewMy addSubview:self.labelTitle];
    self.labelTitle.frame =CGRectMake(0,CGRectGetMaxY(self.myHeader.frame)+5, ScreenWidth, 20);
 
    self.labePhone = [[UILabel alloc]init];
    self.labePhone.textColor = [UIColor whiteColor];
    self.labePhone.font = FONT_(15);
    self.labePhone.textAlignment = NSTextAlignmentCenter;
    [self.tableViewMy addSubview:self.labePhone];
    self.labePhone.frame =CGRectMake(0,CGRectGetMaxY(self.labelTitle.frame)+5, ScreenWidth, 20);
    
    self.labelMinistry = [[UILabel alloc]init];
    self.labelMinistry.textColor = [UIColor whiteColor];
    self.labelMinistry.font = FONT_(15);
    self.labelMinistry.textAlignment = NSTextAlignmentCenter;
    [self.tableViewMy addSubview:self.labelMinistry];
    self.labelMinistry.frame =CGRectMake(0,CGRectGetMaxY(self.labePhone.frame)+5, ScreenWidth, 20);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return arrayTitle.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray * array = arrayTitle[section];
    return array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;

}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 0){
        return 10;
    }else{
        return 70;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(section == 0){
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if(!cell){
            cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
        }
        return cell;
    }else{
        LSZMyFootCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZMyFootCell"];
        [cell.butWithdrawAccount addTarget:self action:@selector(clickButExit) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZMyAndImageViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZMyAndImageViewTableViewCell" forIndexPath:indexPath];
    if(arrayTitle.count > indexPath.section){
        NSArray * array = arrayTitle[indexPath.section];
        if(array.count > indexPath.row){
            NSString * string = array[indexPath.row];
            cell.labelTitle.text = NSStr(string);
            cell.imageViewIcon.image = [UIImage imageNamed:NSStr(string)];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - 滑动视图的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    if (point.y < 0) {
        CGRect rect =self.imageViewHeader.frame;
        rect.origin.y = point.y;
        rect.size.height = - point.y;
        self.imageViewHeader.frame = rect;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        if (indexPath.row == 0){
            LSZCollectionVC * vc = [[LSZCollectionVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1){
            LSZHistoricalRecordVC * vc = [[LSZHistoricalRecordVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 2){
            LSZMyQueryViewController * vc = [[LSZMyQueryViewController alloc]init];
            UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
            backItem.title=@"我的查询";
            self.navigationItem.backBarButtonItem = backItem;
            [self.navigationController pushViewController:vc animated:YES];
//            LSZMyPointsVC * vc = [[LSZMyPointsVC alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 3) {
            LSZSetUpVC * vc = [[LSZSetUpVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        else{
            
        }
        
        
    }else{
        AboutJiukuViewController * vc = [[AboutJiukuViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark -退出账号
-(void)clickButExit
{
    [self goToLogin];
    [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
        NSLog(@"rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);
    } seq:0];
}
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
#pragma mark - 签到
-(void)clickQianDao
{
    NSLog(@"签到");
    if ([NullStringjudgment(NSStr(self.stringAddress)) isEqualToString:@""]) {
        [LSZPublicClassDataToll initWithAlertKuangView:self.view string:@"定位失败无法签到!"];
        return;
    }
    [LSZRequestData usersCheckIn:self.stringAddress :^(BOOL isOK, NSString * string, NSDictionary * dic) {
        if (isOK == YES) {
            [LSZPublicClassDataToll initWithAlertKuangView:self.view string:@"签到成功!"];
        }else{
            [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
        }
    }];
}
-(UITableView *)tableViewMy
{
    if (_tableViewMy == nil) {
        _tableViewMy = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height) style:UITableViewStylePlain];
        _tableViewMy.delegate = self;
        _tableViewMy.dataSource = self;
        _tableViewMy.estimatedRowHeight=0;
        _tableViewMy.estimatedSectionFooterHeight =0;
        _tableViewMy.estimatedSectionHeaderHeight =0;
        _tableViewMy.contentInset = UIEdgeInsetsMake(NAVIGATION_BAR_HEIGHT+150, 0, 0, 0);
        [_tableViewMy setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewMy];
    }
    return _tableViewMy;
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
@end
